<template>
    <el-card>
        <template #header>
            <h3>医疗器械信息</h3>
        </template>
        <!-- 搜索区域 -->
        <el-button type="primary" class="add" @click="showAddWindow()">添加医疗器械</el-button>
        <el-form inline>
            <el-form-item label="医疗器械" style="width: 250px">
                <el-input v-model="params.name"></el-input>
            </el-form-item>
            <el-form-item label="用途" style="">
                <el-input v-model="params.description"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchMedicalDevices()">搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- 表格区域 -->
        <el-table :data="medicalDevices" border @sort-change="handleSortChange">
            <el-table-column label="图片" align="center" width="200">
                <template #default="scope">
                    <el-image v-if="scope.row.device_img" :src="scope.row.device_img"
                        class="row-institution-img"></el-image>
                    <el-image v-else src="src\assets\imgs\暂无图片.png" class="row-institution-img"></el-image>
                </template>
            </el-table-column>
            <el-table-column label="名称" prop="name" sortable="custom" align="center" width="250"></el-table-column>
            <el-table-column label="用途" prop="description" align="center"></el-table-column>
            <el-table-column label="操作" align="center" width="200">
                <template #default="scope">
                    <el-button type="primary" @click="showEditWindow(scope.row)">编辑</el-button>
                    <!-- 删除确认气泡 -->
                    <el-popconfirm width="250" icon-color="#626AEF" title="确认删除该医疗器械吗？">
                        <template #reference>
                            <el-button type="danger">删除</el-button>
                        </template>
                        <template #actions="{ confirm, cancel }">
                            <el-button size="small" @click.once="cancel">取消</el-button>
                            <el-button type="danger" size="small" @click.once="deleteMedicalDevice(scope.row, confirm)">
                                确认
                            </el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination v-if="params.total > 0" :current-page="params.currentPage" :page-size="params.pageSize"
            background layout="total, prev, pager, next" :total="params.total" @current-change="handleCurrentChange" />
    </el-card>
    <!-- 医疗器械信息编辑弹窗 -->
    <el-dialog v-model="showEdit" title="医疗器械信息" width="500" @close="closeWindow">
        <el-form :model="row" label-width="120px" :rules="rules" ref="rowForm">
            <!-- 上传图片 -->
            <el-form-item label="图片" prop="avatar">
                <el-upload class="avatar-uploader" action="http://127.0.0.1:8082/upload" :show-file-list="false"
                    :on-success="handleAvatarSuccess">
                    <el-image v-if="row.device_img" :src="row.device_img" class="institution-img"></el-image>
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="row.name"></el-input>
            </el-form-item>
            <el-form-item label="简介" prop="description">
                <el-input v-model="row.description"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button v-if="!showAdd" type="primary" @click="updateMedicalDevice(row)">保存</el-button>
                <el-button v-if="showAdd" type="primary" @click="addMedicalDevice(row)">添加</el-button>
                <el-button @click="showEdit = false">取消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script>
import { medicalDeviceAddAPI, medicalDevicesFindAPI, medicalDeviceUpdateAPI, medicalDeviceDeleteAPI } from '@/api/medicalDevice';
import { ElMessage } from 'element-plus';
export default {
    data() {
        return {
            params: {
                // 搜索关键词
                name: '',
                description: '',
                // 分页参数
                total: 0,
                currentPage: 1,
                pageSize: 7
            },
            medicalDevices: [],
            showEdit: false,
            showAdd: false,
            row: {},
            rules: {
                name: { required: true, message: '名称不能为空', trigger: 'blur' },
                description: { required: true, message: '简介不能为空', trigger: 'blur' },
            }
        }
    },
    methods: {
        loadMedicalDeviceInfoList() {
            medicalDevicesFindAPI(this.params).then((resp) => {
                if (resp.data.code == 2000) {
                    this.medicalDevices = resp.data.datas;
                    this.params.total = resp.data.total;
                } else {
                    ElMessage.error(resp.data.message);
                }
            })
        },
        searchMedicalDevices() {
            this.loadMedicalDeviceInfoList();
        },
        handleCurrentChange(page) {
            this.params.currentPage = page;
            this.loadMedicalDeviceInfoList();
        },
        // 图片上传成功之后回调函数
        handleAvatarSuccess(response) {
            this.row.device_img = response;
        },
        showEditWindow(row) {
            this.row = JSON.parse(JSON.stringify(row));
            this.showEdit = true;
        },
        updateMedicalDevice(row) {
            this.$refs.rowForm.validate((val) => {
                if (val) {
                    medicalDeviceUpdateAPI(row).then((resp) => {
                        if (resp.data.code == 2000) {
                            this.loadMedicalDeviceInfoList();
                            ElMessage.success(resp.data.message);
                        } else {
                            ElMessage.error(resp.data.message);
                        }
                    })
                }
            })
            this.showEdit = false
        },
        deleteMedicalDevice(row, confirm) {
            medicalDeviceDeleteAPI(row).then((resp) => {
                if (resp.data.code == 2000) {
                    this.loadMedicalDeviceInfoList();
                    ElMessage.success(resp.data.message);
                } else {
                    ElMessage.error(resp.data.message);
                }
            })
            confirm();
        },
        showAddWindow() {
            this.row = {};
            this.showEdit = true;
            this.showAdd = true;
        },
        addMedicalDevice(row) {
            row.role = 2;
            console.log(row);

            this.$refs.rowForm.validate((val) => {
                if (val) {
                    medicalDeviceAddAPI(row).then((resp) => {
                        if (resp.data.code == 2000) {
                            this.loadMedicalDeviceInfoList();
                            ElMessage.success(resp.data.message);
                        } else {
                            ElMessage.error(resp.data.message);
                        }
                    })
                }
            })
            this.showEdit = false;
            this.showAdd = false;
        },
        closeWindow() {
            this.loadMedicalDeviceInfoList();
            this.showEdit = false;
            this.showAdd = false;
        },
        handleSortChange({ prop, order }) {
            if (order) {
                // 根据 prop 和 order 进行排序
                this.params.sortProp = prop;
                this.params.sortOrder = order === 'ascending' ? 'asc' : 'desc';
                this.loadMedicalDeviceInfoList();
            } else {
                // 清除排序
                delete this.params.sortProp;
                delete this.params.sortOrder;
                this.loadMedicalDeviceInfoList();
            }
        }
    },
    created() {
        this.loadMedicalDeviceInfoList();
    }
}
</script>

<style scoped>
.el-pagination {
    margin: 20px 0 0 450px;
    align-items: center;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}

.institution-img {
    width: 300px;
    height: 200px;
}

.add {
    position: relative;
    top: -10px;
}

.row-institution-img {
    width: 150px;
    height: 100px;
}
</style>